use warp::{http::StatusCode, reject::reject, Filter, Rejection, Reply};
use serde::Deserialize;
use jsonwebtoken::{encode, decode, Header, Validation, Algorithm, EncodingKey, DecodingKey};
use bcrypt::{hash, verify, DEFAULT_COST};
use diesel::prelude::*;
use crate::config::settings::Settings;
use crate::db::models::User;
use crate::db::connection::establish_connection;
// TOBE ADDED
// XXX

// defined an structure
#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    uername: String,
    password: String,
}

// process LoginRequest
// TODO get request, verify and return JWT 
async fn handle_login (
    settings: Settings,
    login_request: LoginRequest,

    ) -> Result<impl Reply, Rejection> {
    // Don't forget db connection
    use crate::db::*;
    let connection = establish_connection();
    let user = get_user_by_username(&connection, &login_request.username)
        .optional()
        .unwrap_or(None);

    match user {
        // HACK: 
        Some(user) if verify( ).unwrap() => {

        },
        _ => Err(),
    }
}

// process jwt
fn generate_jwt() {}

// route
let login_route = warp::post()

// process error
#[derive(Debug, thiserror::Error)]
pub enum LoginError {
    #[error("Invalid credentials")]
    InvalidCredentials,
    // TODO
}
